<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var numSubarraysWithSum = function (nums, goal) {
      let count = 0
      for (let i = 0; i < nums.length - 1; i++) {
        ans = 0
        ans += nums[i]
        if (ans == goal) {
          count++
        }
        for (let j = i + 1; j < nums.length; j++) {
          ans += nums[j]
          if (ans == goal) {
            count++
          }
        }
      }
      if (nums[nums.length - 1] == goal) {
        count++
      }
      return count
    };
    console.log(numSubarraysWithSum(nums = [1, 0, 1, 0, 1], goal = 2))
  </script>
</body>

</html>